home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11603 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  63 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!friend!news
  3. From: rich@kastle.com (Richard Krehbiel)
  4. Subject: Re: 12 bit datatype in binary file - misaligned
  5. Message-ID: <1996Mar25.122240.28678@friend.kastle.com>
  6. Sender: news@friend.kastle.com (News)
  7. Reply-To: rich@kastle.com
  8. Organization: Kastle Development Associates
  9. X-Newsreader: Forte Free Agent 1.0.82
  10. References: <4itqv9$kcq@news.ccit.arizona.edu>
  11. Date: Mon, 25 Mar 1996 12:21:16 GMT
  12.  
  13. chrisl@SEDS.LPL.Arizona.EDU (Chris Lewicki) wrote:
  14.  
  15. >Hello All,
  16. >    I've been scouring the net and the bookstores for the last few
  17. >days trying to figure out how to read a 12-bit data length out of a
  18. >binary file.
  19. >    Here's the setup:  I have a data file that has all lengths of
  20. >fields in it.  There are 1, 2, 3, 4, 6, 8, 12, and 16 bit data types
  21. >all packed in very efficiently.  I've figured out that you can define
  22. >a structure:
  23.  
  24. >struct BitData {
  25. >    unsigned onebit : 1;    
  26. >    unsigned anotheronebit : 1;
  27. >    unsigned fourbit : 4;
  28. >    unsigned twobit : 2;
  29. >    unsigned : 4; /* 4 bits padding */
  30. >    unsigned char eightbit;
  31. >    unsigned twelvebit : 12;
  32. >    unsigned sixteenbit;
  33. >};
  34.  
  35. >struct BitData bitdata;
  36.  
  37. >    Using this I could read in a file formatted this way with a simple 
  38. >fread(&bitdata, ...) but I find that I encounter problems when I get
  39. >to the 12 bit data types.  It will read in the first 12 bit variable
  40. >encountered, but everything after that is misaligned.  I've searched
  41. >the world over, but I can't find a thing on this topic.        
  42. >    I understand that this is implementation dependent, and I am
  43. >using gcc 2.7.2 (and/or SUNWspro cc) on a Sparc 10 running Solaris
  44. >2.5.  I've tried the Sun cc compiled using the -misalign compile flag,
  45. >but that doesn't work either.
  46.  
  47. I think the "unsigned char eightbit;" is your problem.  Did you want
  48. to say "unsigned eightbit : 8;" here?  Just because char happens to be
  49. 8 bits does not mean a struct member of type char will be placed and
  50. aligned the same as a :8 bit field.  A char member must be on an
  51. addressable boundary (a program may take it's address with the &
  52. operator), whereas an 8 bit field need not be so aligned.
  53.  
  54. (And of course it's good that you know this is all implementation-
  55. dependent and how big a risk you are taking by depending on bit fields
  56. in external storage.  I recall when Lattice C changed their bit field
  57. ordering and alignment rules between compiler versions.)
  58.  
  59. --
  60. Richard Krehbiel, Kastle Systems, Arlington VA USA
  61. rich@kastle.com (work) or richk@mnsinc.com (personal)
  62.  
  63.